<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="父级权限id，顶级权限为-1" prop="parentId">-->
<!--        <el-input-->
<!--          v-model="queryParams.parentId"-->
<!--          placeholder="请输入父级权限id，顶级权限为-1"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="菜单名称" prop="resourceName">
        <el-input
          v-model="queryParams.resourceName"
          placeholder="请输入菜单名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="菜单编码" prop="resourceCode">
        <el-input
          v-model="queryParams.resourceCode"
          placeholder="请输入菜单编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="创建者id" prop="createUser">-->
<!--        <el-input-->
<!--          v-model="queryParams.createUser"-->
<!--          placeholder="请输入创建者id"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="更新人id" prop="updateUser">-->
<!--        <el-input-->
<!--          v-model="queryParams.updateUser"-->
<!--          placeholder="请输入更新人id"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="删除标记" prop="deleted">-->
<!--        <el-input-->
<!--          v-model="queryParams.deleted"-->
<!--          placeholder="请输入删除标记"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="显示顺序" prop="orderNum">-->
<!--        <el-input-->
<!--          v-model="queryParams.orderNum"-->
<!--          placeholder="请输入显示顺序"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="请求地址" prop="url">-->
<!--        <el-input-->
<!--          v-model="queryParams.url"-->
<!--          placeholder="请输入请求地址"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="打开方式" prop="target">-->
<!--        <el-input-->
<!--          v-model="queryParams.target"-->
<!--          placeholder="请输入打开方式"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="菜单状态，" prop="visible">-->
<!--        <el-input-->
<!--          v-model="queryParams.visible"-->
<!--          placeholder="请输入菜单状态，"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="是否刷新(0刷新，1不刷新)" prop="isRefresh">-->
<!--        <el-input-->
<!--          v-model="queryParams.isRefresh"-->
<!--          placeholder="请输入是否刷新(0刷新，1不刷新)"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="图标地址" prop="icon">-->
<!--        <el-input-->
<!--          v-model="queryParams.icon"-->
<!--          placeholder="请输入图标地址"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
	    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:resource:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-sort"
          size="mini"
          @click="toggleExpandAll"
        >展开/折叠</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="resourceList"
      row-key="id"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
<!--      <el-table-column label="父级权限id，顶级权限为-1" prop="parentId" />-->
      <el-table-column label="菜单名称" align="center" prop="resourceName" />
      <el-table-column label="菜单英文名称" align="center" prop="resourceEnName" />
      <el-table-column label="组件路径" align="center" prop="component" />
      <el-table-column label="重定向" align="center" prop="redirect" />
      <el-table-column label="组件关键字" align="center" prop="keyName" />
      <el-table-column label="菜单类型" align="center" prop="resourceType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.menu_resource_type" :value="scope.row.resourceType"/>
        </template>
      </el-table-column>
      <el-table-column label="菜单编码" align="center" prop="resourceCode" />
<!--      <el-table-column label="创建者id" align="center" prop="createUser" />-->
<!--      <el-table-column label="更新人id" align="center" prop="updateUser" />-->
<!--      <el-table-column label="删除标记" align="center" prop="deleted" />-->
      <el-table-column label="排序序号" align="center" prop="orderNum" />
      <el-table-column label="请求地址" align="center" prop="url" />
      <el-table-column label="打开方式" align="center" prop="target">
        <template slot-scope="scope">
          <dict-tag :showValue="false" :options="dict.type.menu_open_target" :value="scope.row.target"/>
        </template>
      </el-table-column>
      <el-table-column label="是否显示" align="center" prop="visible">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.menu_show_status" :value="scope.row.visible"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="是否刷新(0刷新，1不刷新)" align="center" prop="isRefresh" />-->
      <el-table-column label="菜单图标" align="center" prop="icon">
        <template slot-scope="scope">
          <el-image :src="scope.row.icon" :preview-src-list="[scope.row.icon]" class="el-avatar">
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline"></i>
            </div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:resource:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['system:resource:add']"
          >新增</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:resource:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改忆图菜单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="父级" prop="parentId">
          <treeselect v-model="form.parentId" :options="resourceOptions" :normalizer="normalizer" placeholder="请选择父级权限id，顶级权限为-1" />
        </el-form-item>
        <el-form-item label="菜单名称" prop="resourceName">
          <el-input v-model="form.resourceName" placeholder="请输入菜单名称" />
        </el-form-item>
        <el-form-item label="菜单英文名称" prop="resourceEnName">
          <el-input v-model="form.resourceEnName" placeholder="菜单英文名称" />
        </el-form-item>
        <el-form-item label="组件路径" prop="component">
          <el-input v-model="form.component" placeholder="请输入组件路径" />
        </el-form-item>
        <el-form-item label="重定向" prop="redirect">
          <el-input v-model="form.redirect" placeholder="请输入重定向" />
        </el-form-item>
        <el-form-item label="路由组件关键字" prop="keyName">
          <el-input v-model="form.keyName" placeholder="请输入路由关键字" />
        </el-form-item>
        <el-form-item label="菜单编码" prop="resourceCode">
          <el-input v-model="form.resourceCode" placeholder="请输入菜单编码" />
        </el-form-item>
        <el-form-item label="菜单类型" prop="resourceType">
          <el-radio-group v-model="form.resourceType">
            <el-radio v-for="d in dict.type.menu_resource_type" :value="d.value" :label="d.value" :key="d.value">
              {{ d.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
<!--        <el-form-item label="创建者id" prop="createUser">-->
<!--          <el-input v-model="form.createUser" placeholder="请输入创建者id" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="更新人id" prop="updateUser">-->
<!--          <el-input v-model="form.updateUser" placeholder="请输入更新人id" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="删除标记" prop="deleted">-->
<!--          <el-input v-model="form.deleted" placeholder="请输入删除标记" />-->
<!--        </el-form-item>-->
        <el-form-item label="排序序号" prop="orderNum">
          <el-input v-model="form.orderNum" placeholder="请输入显示顺序" />
        </el-form-item>
        <el-form-item label="请求地址" prop="url">
          <el-input v-model="form.url" placeholder="请输入请求地址" />
        </el-form-item>
        <el-form-item label="打开方式" prop="target">
          <el-radio-group v-model="form.target">
            <el-radio v-for="d in dict.type.menu_open_target" :value="d.value" :label="d.value" :key="d.value">
              {{ d.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否显示" prop="visible">
          <el-radio-group v-model="form.visible">
            <el-radio v-for="d in dict.type.menu_show_status" :value="d.value" :label="d.value" :key="d.value">
              {{ d.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
<!--        <el-form-item label="是否刷新(0刷新，1不刷新)" prop="isRefresh">-->
<!--          <el-input v-model="form.isRefresh" placeholder="请输入是否刷新(0刷新，1不刷新)" />-->
<!--        </el-form-item>-->
        <el-form-item label="菜单图标" prop="icon">
          <image-upload :limit="1" v-model="form.icon"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listResource, getResource, delResource, addResource, updateResource } from "@/api/resource";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Resource",
  components: {
    Treeselect
  },
  dicts:[
    'menu_show_status',
    'menu_resource_type',
    'menu_open_target'
  ],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 忆图菜单表格数据
      resourceList: [],
      // 忆图菜单树选项
      resourceOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        parentId: null,
        resourceName: null,
        resourceEnName: null,
        keyName: null,
        resourceType: null,
        resourceCode: null,
        createUser: null,
        updateUser: null,
        deleted: null,
        orderNum: null,
        url: null,
        target: null,
        visible: null,
        isRefresh: null,
        icon: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        parentId: [
          { required: true, message: "父级权限id，顶级权限为-1不能为空", trigger: "blur" }
        ],
        resourceName: [
          { required: true, message: "菜单名称不能为空", trigger: "blur" }
        ],
        resourceType: [
          { required: true, message: "菜单类型，1目录，2菜单，3按钮不能为空", trigger: "change" }
        ],
        resourceCode: [
          { required: true, message: "菜单编码不能为空", trigger: "blur" }
        ],
        createUser: [
          { required: true, message: "创建者id不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询忆图菜单列表 */
    getList() {
      this.loading = true;
      listResource(this.queryParams).then(response => {
        this.resourceList = this.handleTree(response.data, "id", "parentId");
        this.loading = false;
      });
    },
    /** 转换忆图菜单数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.resourceName,
        children: node.children
      };
    },
	/** 查询忆图菜单下拉树结构 */
    getTreeselect() {
      listResource().then(response => {
        this.resourceOptions = [];
        const data = { id: 0, resourceName: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, "id", "parentId");
        this.resourceOptions.push(data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        parentId: null,
        resourceName: null,
        resourceEnName: null,
        component: null,
        redirect: null,
        keyName: null,
        resourceType: null,
        resourceCode: null,
        createUser: null,
        updateUser: null,
        createTime: null,
        updateTime: null,
        deleted: null,
        orderNum: null,
        url: null,
        target: null,
        visible: null,
        isRefresh: null,
        icon: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getTreeselect();
      if (row != null && row.id) {
        this.form.parentId = row.id;
      } else {
        this.form.parentId = 0;
      }
      this.open = true;
      this.title = "添加忆图菜单";
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      if (row != null) {
        this.form.parentId = row.parentId;
      }
      getResource(row.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改忆图菜单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateResource(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addResource(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除忆图菜单编号为"' + row.id + '"的数据项？').then(function() {
        return delResource(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
